package com.kye.express.business.order.order_address;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kye.express.R;
import com.kye.express.databinding.ItemAddressBinding;
import com.kye.express.view.reclyerview.OnItemClickListener;
import com.kye.model.bean.order.OrderAddresses;

import java.io.Serializable;

import me.drakeet.multitype.ItemViewBinder;
import me.drakeet.multitype.MultiTypeAdapter;

public class AddressViewBinder extends ItemViewBinder<AddressViewBinder.AddressViewBean, AddressViewBinder.ViewHolder> {

    private OnItemClickListener<OrderAddresses.Address> mOnItemClickListener;
    private OnItemClickListener<OrderAddresses.Address> mOnEditItemClickListener;

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_address, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull AddressViewBean item) {
        holder.bind(item);
    }

    @Override
    protected long getItemId(@NonNull AddressViewBean item) {
        return item.getAddress().getGuid().hashCode();
    }

    public OnItemClickListener<OrderAddresses.Address> getOnItemClickListener() {
        return mOnItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener<OrderAddresses.Address> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public OnItemClickListener<OrderAddresses.Address> getOnEditItemClickListener() {
        return mOnEditItemClickListener;
    }

    public void setOnEditItemClickListener(OnItemClickListener<OrderAddresses.Address> onEditItemClickListener) {
        mOnEditItemClickListener = onEditItemClickListener;
    }

    static class AddressViewBean implements Serializable{
        private OrderAddresses.Address mAddress;
        private Boolean selected;

        public AddressViewBean(OrderAddresses.Address address, Boolean selected) {
            mAddress = address;
            this.selected = selected;
        }

        public OrderAddresses.Address getAddress() {
            return mAddress;
        }

        public void setAddress(OrderAddresses.Address address) {
            mAddress = address;
        }

        public Boolean getSelected() {
            return selected;
        }

        public void setSelected(Boolean selected) {
            this.selected = selected;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemAddressBinding mBinding;
        private AddressViewBean mData;

        ViewHolder(View itemView) {
            super(itemView);
            mBinding = ItemAddressBinding.bind(itemView);

            mBinding.relativeLayoutAddresses.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener == null) return;
                    mOnItemClickListener.onClickItem(mData.getAddress(), view);
                }
            });

            mBinding.imageButtonEditAddress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnEditItemClickListener == null) return;
                    mOnEditItemClickListener.onClickItem(mData.getAddress(), view);
                }
            });
        }

        public void bind(AddressViewBean data) {
            mData = data;
            mBinding.setAddress(data.getAddress());
            mBinding.setSelected(data.getSelected());

            int position = getAdapterPosition();

            if (position > 0 &&
                    itemView.getParent() != null &&
                    itemView.getParent() instanceof RecyclerView &&
                    ((RecyclerView) itemView.getParent()).getAdapter() != null &&
                    ((RecyclerView) itemView.getParent()).getAdapter() instanceof MultiTypeAdapter &&
                    ((MultiTypeAdapter) ((RecyclerView) itemView.getParent()).getAdapter()).getItems().get(position - 1) != null &&
                    !(((MultiTypeAdapter) ((RecyclerView) itemView.getParent()).getAdapter()).getItems().get(position - 1) instanceof AddressViewBean)
                    ) {
                mBinding.setFirst(true);
                return;
            }

            mBinding.setFirst(false);
        }
    }
}
